import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '../layout/default.vue'
import {getToken} from '@/utils/auth'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import Config from '@/config'
import loginAlert from './../modal';

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        name: 'Index',
        component: Layout,
        redirect: '/home',
        children: [
            {
                path: '/home',
                name: 'Home',
                component: () => import('../views/Home.vue'),
                meta: {title: '课程', needLogin: true}
            },
			{
			    path: '/search',
			    name: 'search',
			    component: () => import('../views/course/search.vue'),
			    meta: {title: '课程搜索'}
			},
            {
                path: '/course/class',
                name: 'Calss',
                component: () => import('../views/course/class.vue'),
                meta: {title: '课程', needLogin: true}
            },
            {
                path: '/course/info',
                name: 'Info',
                component: () => import('../views/course/info.vue'),
                meta: {NavBar: true, title: '题库', needLogin: true}
            },
            {
                path: '/course/exam',
                name: 'Exam',
                component: () => import('../views/course/exam.vue'),
                meta: {NavBar: true, title: '考试', needLogin: true}
            },
            {
                path: '/userInfo',
                name: 'UserInfo',
                component: () => import('../views/user/info.vue'),
                meta: {NavBar: true, title: '个人中心', needLogin: true}
            },
            {
                path: '/course/detail',
                name: 'courseDetail',
                component: () => import('../views/course/detail.vue'),
				meta: {NavBar: true, title: '课程详情', needLogin: true}
            },
            {
                path: '/bank/practiceDetails',
                name: 'practiceDetails',
                component: () => import('../views/bank/practiceDetails.vue')
            },
            {
                path: '/bank/manyErrorQue',
                name: 'manyErrorQue',
                component: () => import('../views/bank/manyErrorQue.vue')
            },
            {
                path: '/bank/bankSearch',
                name: 'bankSearch',
                component: () => import('../views/bank/bankSearch.vue')
            },
        ]
    },
    {
        path: '/login',
        name: 'Login',
        component: () => import('../views/login.vue'),
		meta: {title: '用户登录'}
    },
    {
        path: '/404',
        component: () => import('@/views/features/404'),
        hidden: true
    },
    {
        path: '/401',
        component: () => import('@/views/features/401'),
        hidden: true
    },
    {
        path: '/redirect',
        component: Layout,
        hidden: true,
        children: [
            {
                path: '/redirect/:path*',
                component: () => import('@/views/features/redirect')
            }
        ]
    }
]

const router = new VueRouter({
    routes
})

const whiteList = ['/login']// no redirect whitelist

router.beforeEach((to, from, next) => {
	NProgress.start()
 //    if (to.meta.needLogin) {
	// 	if(!getToken()){
	// 		//弹窗登录
	// 		// loginAlert.install({
	// 		// 	//  dialog: true  // 这里传入的参数就是刚刚封装的MyModal里面的data参数
	// 		//   })
	// 		next({path: '/'})
	// 		NProgress.done()
	// 	}else{
 //            next()
 //        }
 //    }else{
	// 	next()
	// }
	if(getToken()){
		if (to.path === '/login') {
		  next({ path: '/' })
		  NProgress.done() // if current page is dashboard will not trigger	afterEach hook, so manually handle it
		}else{
			next()
		}
	}else{
	    /* has no token*/
	    if (whiteList.indexOf(to.path) !== -1) { // 在免登录白名单，直接进入
	      next()
	    } else {
	      next(`/login?redirect=${to.path}`) // 否则全部重定向到登录页
	      NProgress.done()
	    }
	}
	NProgress.done()
    if (to.meta.title) {
        document.title = to.meta.title + ' - ' + Config.webName
    }
})
// 重复点击导航，控制台出错
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

export default router
